﻿using ScottPlot.Avalonia;
using Digitalmes.Core.Services;
using ScottPlot;

namespace Digitalmes.AvaloniaUI.ViewModels;

public sealed partial class TurntablePlotViewModel : ViewModelBase
{
    private readonly ITurntableArchiveService _turntableArchiveService;

    // 查询--开始时间
    [ObservableProperty]
    private DateTime? _startDate;

    // 查询--结束时间
    [ObservableProperty]
    private DateTime? _endDate;

    public AvaPlot PlotControl1 { get; }

    public AvaPlot PlotControl2 { get; }

    public AvaPlot PlotControl3 { get; }

    public AvaPlot PlotControl4 { get; }

    // 查询
    [RelayCommand]
    private async Task Query()
    {
        // 查询数据
        var data = await _turntableArchiveService.GetListAsync(StartDate, EndDate);

        double[] dataY1 = data.Select(m => m.Speed).ToArray();
        BindLinePlot(PlotControl1, dataY1);

        int[] dataY2 = data.Select(m => m.Position).ToArray();
        BindLinePlot(PlotControl2, dataY2);

        double[] dataY3 = data.Select(m => m.ForwardRotationTime).ToArray();
        double[] dataY4 = data.Select(m => m.ReverseRotationTime).ToArray();
        BindMultiLinePlot(PlotControl3, new List<(string, double[])>
        {
            ("正转时间", dataY3),
            ("反转时间", dataY4),
        });

        double[] dataY5 = data.Select(m => m.DistanceSensor).ToArray();
        BindLinePlot(PlotControl4, dataY5);
    }

    public TurntablePlotViewModel(ITurntableArchiveService turntableArchiveService)
    {
        _turntableArchiveService = turntableArchiveService;

        PlotControl1 = new AvaPlot();
        PlotControl1.Plot.Title("转速");
        PlotControl1.Plot.YLabel("转速(RPM)");
        PlotControl1.Plot.Font.Automatic();

        PlotControl2 = new AvaPlot();
        PlotControl2.Plot.Title("转速");
        PlotControl2.Plot.YLabel("转速(RPM)");
        PlotControl2.Plot.Font.Automatic();

        PlotControl3 = new AvaPlot();
        PlotControl3.Plot.Title("正转/反转时间");
        PlotControl3.Plot.YLabel("时间(s)");
        PlotControl3.Plot.Font.Automatic();

        PlotControl4 = new AvaPlot();
        PlotControl4.Plot.Title("到位距离传感器值");
        PlotControl4.Plot.YLabel("测距(mm)");
        PlotControl4.Plot.Font.Automatic();
    }

    // 绑定 Line 类型 Plot
    private static void BindLinePlot<T>(AvaPlot avaPlot, T[] dataY)
        where T : struct
    {
        avaPlot.Plot.Clear();
        avaPlot.Plot.Add.Signal(dataY);
        avaPlot.Plot.Axes.AutoScale(); // reset limits to fit the data
        avaPlot.Refresh(); // 立即刷新图表
    }

    private static void BindMultiLinePlot<T>(AvaPlot avaPlot, IEnumerable<(string legend, T[] dataY)> dataYList)
       where T : struct
    {
        avaPlot.Plot.Clear();
        foreach (var (legend, dataY) in dataYList)
        {
            var sig = avaPlot.Plot.Add.Signal(dataY);
            sig.LegendText = legend;
        }
        avaPlot.Plot.ShowLegend();
        avaPlot.Plot.Axes.AutoScale(); // reset limits to fit the data
        avaPlot.Refresh(); // 立即刷新图表
    }
}
